class Solution {
public:
    bool isHappy(int n) {
        int slow = n;
        int fast = bitSum(n);
        while (slow != fast)
        {
            slow = bitSum(slow);
            fast = bitSum(bitSum(fast));
        }

        return slow == 1;
    }
    int bitSum(int n)
    {
        int result = 0;
        while (n > 0)
        {
            int temp = n % 10;
            result += temp * temp;
            n = n / 10;
        }
        return result;
    }
};